/*
 * @Author: lclangcheng
 * @Date: 2018-11-01 14:26:20
 * @Last Modified by: lclangcheng
 * @Last Modified time: 2019-11-12 15:54:24
 */

'use strict'

const log = require('../libs/logger')
const BaseComponent = require('./base_component')
const { PictureService, CategoryService } = require('../service')

class Category extends BaseComponent {
  constructor () {
    super()
    this.name = 'Picture'
    this.getById = this.getById.bind(this)
    this.getList = this.getList.bind(this)
    this.getListInPicTag = this.getListInPicTag.bind(this)
    this.getListByTagId = this.getListByTagId.bind(this)
    this.getRelateByPicId = this.getRelateByPicId.bind(this)
    this.getLatestTagList = this.getLatestTagList.bind(this)
    this.download = this.download.bind(this)
  }
  filterList(res) {
    res.forEach(pic => {
      pic.tag = (pic.tag && pic.tag.length) ? pic.tag : []   
    })
  }
  /**
   * 获取列表
   * @param {Object} ctx
   * @param {Function} next
   */
  async getById (ctx, next) {
    log.handleLogger(ctx, this.name, this.getById.name)
    const query = ctx.request.query
    try {
      const res = await PictureService.getById({ id: query.id })
      const tags = await PictureService.getPicActiveTags({ id: query.id })
      res.tags = tags
      ctx.body = { status: 200, data: res }
      next && await next()
    } catch (err) {
      log.errHandleLogger(ctx, this.name, this.getList.name, err)
      const result = {
        status: err.status || 500,
        message: err.status ? err.message : '服务器内部错误'
      }
      ctx.body = result
    }
  }

  /**
   *
   * 获取列表
   * @param {Object} ctx
   * @param {Function} next
   */
  async getList (ctx, next) {
    log.handleLogger(ctx, this.name, this.getList.name)
    const query = ctx.request.query
    try {
      const res = await PictureService.getList({ page: query.page, page_size: query.page_size })
      const [total] = await PictureService.getListTotal()
      this.filterList(res)
      ctx.body = { status: 200, data: { list: res, total: total['total'] } }
      next && await next()
    } catch (err) {
      log.errHandleLogger(ctx, this.name, this.getList.name, err)
      const result = {
        status: err.status || 500,
        message: err.status ? err.message : '服务器内部错误'
      }
      ctx.body = result
    }
  }

  /**
   *
   * 获取列表
   * @param {Object} ctx
   * @param {Function} next
   */
  async getRelateByPicId (ctx, next) {
    log.handleLogger(ctx, this.name, this.getRelateByPicId.name)
    const query = ctx.request.query
    try {
      let tids = await PictureService.getTabIdsByPicId({ id: query.pid })
      tids = tids && tids.length ? tids : [0]
      const res = await PictureService.getRelateByPicId({ page: query.page, page_size: query.page_size, tids: tids, id: query.pid })
      this.filterList(res)
      let [total] = await PictureService.getRelateByPicIdTotal({tids: tids, id: query.pid })
      ctx.body = { status: 200, data: { list: res, total: total['total'] } }
      next && await next()
    } catch (err) {
      log.errHandleLogger(ctx, this.name, this.getList.name, err)
      const result = {
        status: err.status || 500,
        message: err.status ? err.message : '服务器内部错误'
      }
      ctx.body = result
    }
  }

  /**
   * 获取列表通过tagId
   * @param {Object} ctx
   * @param {Function} next
   */
  async getListByTagId (ctx, next) {
    log.handleLogger(ctx, this.name, this.getListByTagId.name)
    const query = ctx.request.query
    try {
      const res = await PictureService.getListByTagId({ page: query.page, page_size: query.page_size, tid: query.tid })
      const [total] = await PictureService.getListByTagIdTotal({ tid: query.tid })
      this.filterList(res)
      ctx.body = { status: 200, data: { list: res, total: total['total'] } }
      next && await next()
    } catch (err) {
      log.errHandleLogger(ctx, this.name, this.getList.name, err)
      const result = {
        status: err.status || 500,
        message: err.status ? err.message : '服务器内部错误'
      }
      ctx.body = result
    }
  }

  /**
   * 获取列表
   * @param {Object} ctx
   * @param {Function} next
   */
  async getListInPicTag (ctx, next) {
    log.handleLogger(ctx, this.name, this.getListInPicTag.name)
    const query = ctx.request.query
    try {
      let res = []
      let tids = []
      if (query.type === 'category') { // 查询类别
        let ids = await CategoryService.getCateListTagIds({ cat_ids: query.ids })
        tids = ids && ids.length ? ids : [0]
      } else {
        tids = query.ids
      }
      res = await PictureService.getListInPicTag({ page: query.page, page_size: query.page_size, tids: tids })
      const [total] = await PictureService.getListInPicTagTotal({ tids: tids })
      this.filterList(res)
      ctx.body = { status: 200, data: { list: res, total: total['total'] } }
      next && await next()
    } catch (err) {
      log.errHandleLogger(ctx, this.name, this.getList.name, err)
      const result = {
        status: err.status || 500,
        message: err.status ? err.message : '服务器内部错误'
      }
      ctx.body = result
    }
  }

  /*
  *获取最新标签
  */
 async getLatestTagList (ctx, next) {
  log.handleLogger(ctx, this.name, this.getLatestTagList.name)
  try {
    const res = await PictureService.getLatestTagList({})
    ctx.body = { status: 200, data: res }
    next && await next()
  } catch (err) {
    log.errHandleLogger(ctx, this.name, this.getLatestTagList.name, err)
    const result = {
      status: err.status || 500,
      message: err.status ? err.message : '服务器内部错误'
    }
    ctx.body = result
  }
}


  /*
  *下载
  */
  async download (ctx, next) {
    log.handleLogger(ctx, this.name, this.download.name)
    const query = ctx.request.query
    try {
      const res = await PictureService.download({ name: query.name, req: ctx.req })
      ctx.body = { status: 200, data: res }
      next && await next()
    } catch (err) {
      log.errHandleLogger(ctx, this.name, this.getList.name, err)
      const result = {
        status: err.status || 500,
        message: err.status ? err.message : '服务器内部错误'
      }
      ctx.body = result
    }
  }

}

module.exports = new Category()
